﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OrganizationName.Factory.LifeTimeContracts
{
    public class WeakReferenceLifeTimeContract : ILifeTimeContract
    {
        private WeakReference _Instance;

        public bool IsValid
        {
            get
            {
                return _Instance != null && _Instance.IsAlive;
            }
        }

        public object Instance
        {
            get
            {
                return _Instance.Target;
            }
            set
            {
                lock (this)
                {
                    _Instance = new WeakReference(value, false);
                }
            }
        }


        public object SyncRoot
        {
            get
            {
                return this;
            }
        }
    }
}
